using System;
using Common;
using ObjectPool;

namespace Entity
{
    public sealed partial class EntityManager
    {
        /// <summary>
        /// 实体实例对象
        /// </summary>
        private sealed class EntityInstanceObject : ObjectBase
        {
            /// <summary>
            /// 实体资源
            /// </summary>
            private object m_EntityAssets;
            /// <summary>
            /// 实体辅助器
            /// </summary>
            private IEntityHelper m_EntityHelper;

            public EntityInstanceObject()
            {
                m_EntityAssets = null;
                m_EntityHelper = null;
            }

            public static EntityInstanceObject Create(string name, object entityAsset, object entityInstance,
                IEntityHelper entityHelper)
            {
                if (entityAsset == null)
                    throw new Exception("Entity asset is invalid.");
                if (entityHelper == null)
                    throw new Exception("Entity helper is invalid.");
                
                EntityInstanceObject entityInstanceObject = ReferencePool.Acquire<EntityInstanceObject>();
                entityInstanceObject.Initialize(name, entityInstance);
                entityInstanceObject.m_EntityAssets = entityAsset;
                entityInstanceObject.m_EntityHelper = entityHelper;
                return entityInstanceObject;
            }


            public override void Clear()
            {
                base.Clear();
                m_EntityAssets = null;
                m_EntityHelper = null;
            }


            protected internal override void Release(bool isShutdown)
            {
                m_EntityHelper.ReleaseEntity(m_EntityAssets, this);
            }
        }
    }
}